﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_learn : AdminPageBase
{
    private string returnURL;
    protected int planfinishid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        if (Request.QueryString["returnURL"] != null)
            returnURL = Request.QueryString["returnURL"].ToString();
        GetPlanFinishId();
        if (!IsPostBack)
        {
            GetPlanFinishClass();
        }
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }


    }
    protected void GetPlanFinishId()
    {
        if (Request.QueryString["id"] != null)
        {
            planfinishid = Convert.ToInt32(Request.QueryString["id"]);

        }
    }
    protected void GetPlanFinishClass()
    {

        if (planfinishid != 0)
        {
            if (AdminSessionHelper.UserDetails != null)
            {
                DataSet ds = jtDataAccess.GetStudyClass(planfinishid, Convert.ToInt32(AdminSessionHelper.UserDetails["UserId"].ToString()));
                if (CoreTools.CheckDataSet(ds))
                {

                    lblClassName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                    lblStudyGlobal.Text = ds.Tables[0].Rows[0]["StudyGoals"].ToString();
                    lblStudentObject.Text = ds.Tables[0].Rows[0]["StudentsObject"].ToString();
                    lblStudyMode.Text = ds.Tables[0].Rows[0]["StudyMode"].ToString();
                    lbltime.Text = ds.Tables[0].Rows[0]["Time"].ToString();

                    if (ds.Tables[0].Rows[0]["BackgroundMusic"].ToString() != "")
                    {
                        ViewState["musicURL"] = ds.Tables[0].Rows[0]["BackgroundMusic"].ToString();
                        string playurl = ConfigHelper.MusicUploadPath.Replace("~/", "../../");
                        System.IO.FileInfo file = new System.IO.FileInfo(HttpContext.Current.Request.MapPath(playurl + ds.Tables[0].Rows[0]["BackgroundMusic"].ToString()));
                        if (file.Exists)
                        {

                            lblmusic.Text = Play.PlayBackGroudMusic(playurl + ds.Tables[0].Rows[0]["BackgroundMusic"].ToString());
                        }
                       
                       
                    }
                }
                if (ds != null && ds.Tables.Count > 1 && ds.Tables[1].Rows.Count > 0)
                {

                    repResource.DataSource = ds.Tables[1];
                    repResource.DataBind();


                }
            }
            else
                JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }
        else
        {
            Response.Redirect("Study.aspx");
        }
    }
    protected void lbtnStudy_Click(object sender, EventArgs e)
    {

    }
    //protected void lbtnexam_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("examination.aspx?id=" + planfinishid);
    //}
    protected void repResource_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }
    protected void ibtnExam_Click(object sender, ImageClickEventArgs e)
    {
        if (AdminSessionHelper.UserDetails != null)
        {
            int userid = Convert.ToInt32(AdminSessionHelper.UserDetails["UserId"].ToString());
            int StudyAnswerId = 0;
            StudyAnswerId = jtDataAccess.InsStudyAnswer(planfinishid, userid);
            Response.Redirect("examination.aspx?id=" + planfinishid + "&StudyAnswerId=" + StudyAnswerId);
        }
        else
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
    }
    
    protected void repResource_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.Equals("study"))
        {

            HiddenField hdfUrl = (HiddenField)e.Item.FindControl("hdfUrl");
            HiddenField hdfClassDetailId = (HiddenField)e.Item.FindControl("hdfClassDetailId");
            if (hdfClassDetailId != null && (hdfClassDetailId.Value!="")&&hdfUrl != null && hdfUrl.Value != "")
            {
              
                string playurl = ConfigHelper.VideoUploadPath.Replace("~/", "../../");
                string extensionName = System.IO.Path.GetExtension(hdfUrl.Value);
                string playpageurl = "play.aspx?sc=" + hdfClassDetailId.Value;
                string ClassResourceType = "";
                ClassResourceType = CoreTools.GetClassResourceType(extensionName.ToLower());
                if (ClassResourceType.Equals("swf"))
                {
                    System.IO.FileInfo file = new System.IO.FileInfo(HttpContext.Current.Request.MapPath(ConfigHelper.VideoUploadPath.Replace("~/", "../../") + hdfUrl.Value));
                    if (!file.Exists)
                    {
                       
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("课程资源文件不存在或已被删除!"), false);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "write", "<script>window.open('" + playpageurl + "'); </script>", false);
                    }
                }
                else if (ClassResourceType.Equals("video"))
                {
                    System.IO.FileInfo file = new System.IO.FileInfo(HttpContext.Current.Request.MapPath(ConfigHelper.VideoUploadPath.Replace("~/", "../../") + hdfUrl.Value));
                    if (file.Exists)
                    {
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "write", "<script>window.open('" + playpageurl + "'); </script>", false);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("课程资源文件不存在或已被删除!"), false);
                    }
                }
                else if (ClassResourceType.Equals("officeDocument"))
                {
                    string fpath = ConfigHelper.SWFFilePath.Replace("~/", "../../") + hdfUrl.Value.Replace(extensionName, ".swf");
                    string path = HttpContext.Current.Request.MapPath(ConfigHelper.SWFFilePath.Replace("~/", "../../") + hdfUrl.Value.Replace(extensionName, ".swf"));
                    System.IO.FileInfo file = new System.IO.FileInfo(path);

                    if (file.Exists)
                    {
                       
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "write", "<script>window.open('" + playpageurl + "'); </script>", false);
           
                       
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("课程资源文件不存在或已被删除!"), false);
                    }

                }
                else if (ClassResourceType.Equals("Image"))
                {
                    System.IO.FileInfo file = new System.IO.FileInfo(HttpContext.Current.Request.MapPath(ConfigHelper.ImgUploadPath.Replace("~/", "../../") + hdfUrl.Value));
                    if (file.Exists)
                    {
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "write", "<script>window.open('" + playpageurl + "'); </script>", false);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("课程资源文件不存在或已被删除!"), false);
                    }
                }
            }
            else
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("课程资源文件不存在或已被删除!"), false);
      
        }
    }
    protected void ibtnmusic_Click(object sender, ImageClickEventArgs e)
    {
        if (lblmusic.Text== "")
        {
            if (ViewState["musicURL"] != null)
            {
                string fileName = ViewState["musicURL"].ToString();
                string playurl = ConfigHelper.MusicUploadPath.Replace("~/", "../../");
                System.IO.FileInfo file = new System.IO.FileInfo(HttpContext.Current.Request.MapPath(playurl + fileName));
                if (file.Exists)
                {

                    lblmusic.Text = Play.PlayBackGroudMusic(playurl + fileName);
                }
                else
                    lblmusic.Text = "";
                ibtnmusic.ImageUrl = "../../images/btn_backgroudmusic.png";
            }
        }
        else
        {
            lblmusic.Text ="";
            ibtnmusic.ImageUrl = "../../images/btn_closed_music.png";
        }
    }
}